﻿using System.Collections.Generic;
using System.Linq;

namespace BUS.DAL
{
    public class SanPhamDAL
    {
        internal static List<SanPhamBUS> GetAll()
        {
            var db = DataAccess.Database;

            var result = from p in db.SanPhams
                         orderby p.ID descending
                         select new SanPhamBUS()
                         {
                             ID = p.ID,
                             MaSanPham = p.MaSanPham,
                             TenSanPham = p.TenSanPham,
                             LoaiSanPham = new LoaiSanPhamBUS()
                             {
                                 ID = p.LoaiSanPham.ID,
                                 TenLoai = p.LoaiSanPham.TenLoai
                             },
                             MoTa = p.MoTa,
                             Gia = (decimal)p.Gia,
                             Anh = p.Anh,
                             BaoHanh = p.BaoHanh,                             
                             SoTon = (int)p.SoTon,                             
                         };

            return result.ToList<SanPhamBUS>();
        }
       

        internal static int Insert(SanPhamBUS sanPhamBUS)
        {
            var db = DataAccess.Database;

            var sp = new SanPham()
            {
                MaSanPham = sanPhamBUS.MaSanPham,
                TenSanPham = sanPhamBUS.TenSanPham,
                IDLoaiSanPham = sanPhamBUS.LoaiSanPham.ID,
                MoTa = sanPhamBUS.MoTa,
                Gia = sanPhamBUS.Gia,
                Anh = sanPhamBUS.Anh,
                BaoHanh = sanPhamBUS.BaoHanh,                
            };

            db.SanPhams.InsertOnSubmit(sp);
            db.SubmitChanges();
            return sp.ID;
        }

        internal static List<SanPhamBUS> GetByCate(int idCate)
        {
            var db = DataAccess.Database;

            var result = from p in db.SanPhams
                         where p.LoaiSanPham.ID == idCate
                         select new SanPhamBUS()
                         {
                             ID = p.ID,
                             MaSanPham = p.MaSanPham,
                             TenSanPham = p.TenSanPham,
                             LoaiSanPham = new LoaiSanPhamBUS()
                             {
                                 ID = p.LoaiSanPham.ID,
                                 TenLoai = p.LoaiSanPham.TenLoai
                             },
                             MoTa = p.MoTa,
                             Gia = (decimal)p.Gia,
                             Anh = p.Anh,
                             BaoHanh = p.BaoHanh,
                             SoTon = (int)p.SoTon,                            
                         };

            return result.ToList<SanPhamBUS>();
        }

        internal static SanPhamBUS GetSanPhamByID(int id)
        {
            var db = DataAccess.Database;

            var result = from p in db.SanPhams
                         where p.ID == id
                         select new SanPhamBUS()
                         {
                             ID = p.ID,
                             MaSanPham = p.MaSanPham,
                             TenSanPham = p.TenSanPham,
                             LoaiSanPham = new LoaiSanPhamBUS()
                             {
                                 ID = p.LoaiSanPham.ID,
                                 TenLoai = p.LoaiSanPham.TenLoai
                             },
                             MoTa = p.MoTa,
                             Gia = (decimal)p.Gia,
                             Anh = p.Anh,
                             BaoHanh = p.BaoHanh,
                             SoTon = (int)p.SoTon,                             
                         };

            return result.FirstOrDefault<SanPhamBUS>();
        }

        internal static void Update(SanPhamBUS sanPhamBUS)
        {
            var db = DataAccess.Database;
            var sp = db.SanPhams.SingleOrDefault<SanPham>(p => p.ID == sanPhamBUS.ID);

            sp.MaSanPham = sanPhamBUS.MaSanPham;
            sp.TenSanPham = sanPhamBUS.TenSanPham;
            sp.IDLoaiSanPham = sanPhamBUS.LoaiSanPham.ID;
            // sp.MoTa = sanPhamBUS.MoTa;
            sp.Gia = sanPhamBUS.Gia;
            sp.BaoHanh = sanPhamBUS.BaoHanh;
            sp.Anh = sanPhamBUS.Anh;
                        
            db.SubmitChanges();

        }

        internal static void Delete(SanPhamBUS sanPhamBUS)
        {
            ShopDataContext db = DataAccess.Database;

            var result = db.SanPhams.SingleOrDefault<SanPham>(p => p.ID == sanPhamBUS.ID);

            if (result != null) db.SanPhams.DeleteOnSubmit(result);
            db.SubmitChanges();
        }       

        internal static List<SanPhamBUS> GetTopProducts()
        {
            var db = DataAccess.Database;

            var result = (from p in db.SanPhams
                          select new SanPhamBUS()
                          {
                              ID = p.ID,
                              MaSanPham = p.MaSanPham,
                              TenSanPham = p.TenSanPham,
                              Gia = (decimal)p.Gia,
                              Anh = p.Anh,
                              SoTon = (int)p.SoTon,                              
                              BaoHanh = p.BaoHanh,
                              SoLuong = (int)p.ChiTietDatHangs.Sum(o => o.SoLuong)
                          }).OrderByDescending(sp => sp.SoLuong).Take(6);

            return result.ToList<SanPhamBUS>();
        }

        //internal static List<TonKhoBUS> GetAllProductsQuantites()
        //{
        //    var db = DataAccess.Database;

        //    var result = from p in db.SanPhams
        //                 select new TonKhoBUS()
        //                 {
        //                     SanPham = new SanPhamBUS()
        //                     {
        //                         ID = p.ID,
        //                         MaSanPham = p.MaSanPham,
        //                         TenSanPham = p.TenSanPham,
        //                         LoaiSanPham = new LoaiSanPhamBUS()
        //                         {
        //                             ID = p.LoaiSanPham.ID,
        //                             TenLoai = p.LoaiSanPham.TenLoai
        //                         },
        //                         Gia = (decimal)p.Gia
        //                     },
        //                     SoLuongTonKho = (int)p.SoTon,
        //                     NgayCapNhap = TonKhoDAL.GetNgayCapNhat(p.ID)
        //                 };
        //    return result.ToList<TonKhoBUS>();
        //}
    }
}
